<template>
<el-container>
<el-main>
<el-row>
<el-col >
<el-tabs type="border-card">
<el-tab-pane label="异地存储">

<el-row >
    <el-col :span="24">
    <el-form ref="ftpform" :model="ftpform" size="mini" :label-position="labelPosition" label-width="180px" :validate-on-rule-change="false">
      <el-form-item
        label="类型"
        prop="types"
        :rules="[{ required: true, message: '请选择类型名称', trigger: 'blur' }]"
      >
        <el-select v-model="ftpform.types" placeholder="请选择">
          <el-option
            v-for="item in ftptypes"
            :key="item.value"
            :label="item.value"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="名称"
        prop="name"
        :rules="[{ required: true, message: '请输入名称', trigger: 'blur' }]"
      >
        <el-input v-model="ftpform.name"></el-input>
      </el-form-item>
      <el-form-item
        label="主机地址"
        prop="host"
        :rules="[{ required: true, message: '请输入主机地址', trigger: 'blur' }]"
      >
        <el-input v-model="ftpform.host"></el-input>
      </el-form-item>
      <el-form-item
        label="主机端口"
        prop="port"
        :rules="[{ required: true, message: '请输入主机端口', trigger: 'blur' }]"
      >
        <el-input v-model="ftpform.port"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ftpform.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ftpform.password" show-password></el-input>
      </el-form-item>
      <el-form-item
        label="路径"
        prop="path"
        :rules="[{required: true, message: '请输入 路径', trigger: 'blur' }]"
      >
        <el-input v-model="ftpform.path"></el-input>
      </el-form-item>
      <el-form-item label="断线重连">
        <el-input-number v-model="ftpform.relink" :min="0" :max="10"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$route.query.id == ''" type="primary"  @click="addFtp('ftpform')" >添加异地存储</el-button>
        <el-button v-else type="primary" @click="editFtp('ftpform')">修改异地存储</el-button>
      </el-form-item>
    </el-form>
    </el-col>
</el-row>

    </el-tab-pane>
</el-tabs>
</el-col>
</el-row>
</el-main>
</el-container>
</template>
<script>
export default { 
  data() {
    return {
      labelPosition: "sqlite",
      ftptypes: [{ value: "ftp" }, { value: "sftp" }, { value: "Yserver" }],
      ftpform: { relink: 0 },
    };
  },
  created() {
    if (this.$route.query.id && this.$route.query.id !== "") {
      this.$http.Get(this.$api.ftp + "/" + this.$route.query.id, {}, (res) => {
        this.ftpform = res;
      });
    }
  },
  methods: {
    addFtp(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$router.go(-1)
          this.ftpform["port"] = Number(this.ftpform["port"]);
          this.$http.Post(
            this.$api.ftp,
            this.ftpform,
            () => {
              this.$emit("close");
            },
            "添加远程存储"
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    editFtp(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
              this.$router.go(-1)
          this.ftpform["port"] = Number(this.ftpform["port"]);
          this.$http.Put(
            this.$api.ftp + "/" + this.$route.query.id,
            this.ftpform,
            () => {
              this.$emit("close");
            },
            "修改远程存储"
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>